//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.htdata.iiot.db;

import com.htiiot.common.config.imp.ConfigClient;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;

public class DbConnectionPool {
    private static HashMap<String, DbConnectionPool> _poolInstances = new HashMap<String, DbConnectionPool>();
    private HikariDataSource dataSource;
    private String _poolName;

    public static DbConnectionPool getPool() {
        return getPool((String)null);
    }

    public static DbConnectionPool getPool(String poolName) {
        if(poolName == null || poolName.length() == 0) {
            poolName = "maindb";
        }

        if(_poolInstances.containsKey(poolName)) {
            return (DbConnectionPool)_poolInstances.get(poolName);
        } else {
            DbConnectionPool result = null;
            synchronized(_poolInstances) {
                if(!_poolInstances.containsKey(poolName)) {
                    result = new DbConnectionPool(poolName);
                    _poolInstances.put(poolName, result);
                } else {
                    result = (DbConnectionPool)_poolInstances.get(poolName);
                }

                return result;
            }
        }
    }

    public DbConnectionPool() {
        this((String)null);
    }

    public DbConnectionPool(String poolName) {
        if(poolName == null || poolName.length() == 0) {
            poolName = "maindb";
        }

        this._poolName = poolName;
        String dbUrl = ConfigClient.instance().get(poolName, "url");
        //String dbUrl = "jdbc:mysql://47.92.78.233:3306/htiiot?characterEncoding=utf8";
        String dbUser = ConfigClient.instance().get(poolName, "user");
        String dbPassword = ConfigClient.instance().get(poolName, "sec.password");
        //String dbPassword ="HTiiot@123";
        String maxPoolSize = ConfigClient.instance().get(poolName, "maxpoolsize");
        String idleTimeout = ConfigClient.instance().get(poolName, "idletimeout");
        String connectionTimeout = ConfigClient.instance().get(poolName, "connectiontimeout");
        String driverClassName = ConfigClient.instance().get(poolName, "driverclassname");
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(dbUrl);
        if(dbUser != null && dbUser.length() > 0) {
            config.setUsername(dbUser);
        }

        if(dbPassword != null && dbPassword.length() > 0) {
            config.setPassword(dbPassword);
        }

        if(driverClassName != null && driverClassName.length() > 0) {
            config.setDriverClassName(driverClassName);
        }

        if(maxPoolSize != null && maxPoolSize.length() > 0) {
            config.setMaximumPoolSize(Integer.parseInt(maxPoolSize));
        }

        if(idleTimeout != null && idleTimeout.length() > 0) {
            config.setIdleTimeout((long)Integer.parseInt(idleTimeout));
        }

        if(connectionTimeout != null && connectionTimeout.length() > 0) {
            config.setConnectionTimeout((long)Integer.parseInt(connectionTimeout));
        }

        config.addDataSourceProperty("cachePrepStmts", "true");
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
        this.setDataSource(new HikariDataSource(config));
    }

    public Connection getConnection() {
        Connection con = null;

        try {
            con = this.getDataSource().getConnection();
        } catch (SQLException var3) {
            var3.printStackTrace();
        }

        return con;
    }

    public void close() {
        this.dataSource.close();
        synchronized(_poolInstances) {
            if(_poolInstances.containsKey(this._poolName)) {
                _poolInstances.remove(this._poolName);
            }

        }
    }

    public HikariDataSource getDataSource() {
        return this.dataSource;
    }

    private void setDataSource(HikariDataSource dataSource) {
        this.dataSource = dataSource;
    }
}
